﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GSdk.Net.Lglcd
{
    /// <summary>
    /// This class contains reference to devices that the applet can access to.
    /// </summary>
    public class DefaultDeviceCollection : DeviceCollection
    {
        private BlackAndWhiteDevice m_BlackAndWhite;
        private QvgaDevice m_Qvga;

        public bool Disposed { get; private set; }

        public DefaultDeviceCollection(Applet parent)
            : base(parent)
        {
            m_BlackAndWhite = new BlackAndWhiteDevice(Parent);
            m_Qvga = new QvgaDevice(Parent);
        }

        public Device BlackAndWhite
        {
            get
            {
                if (Disposed)
                    throw new ObjectDisposedException("DefaultDeviceCollection");

                if (!Parent.SupportedDevices.HasFlag(DeviceTypes.BlackAndWhite))
                    throw new AppletException("Applet doesn't support black and white device");

                return m_BlackAndWhite;
            }
        }

        public Device Qvga
        {
            get
            {
                if (Disposed)
                    throw new ObjectDisposedException("DefaultDeviceCollection");

                if (!Parent.SupportedDevices.HasFlag(DeviceTypes.Qvga))
                    throw new AppletException("Applet doesn't support qvga device");

                return m_Qvga;
            }
        }

        public override Device this[DeviceTypes device]
        {
            get
            {
                if (Disposed)
                    throw new ObjectDisposedException("DefaultDeviceCollection");

                foreach (DeviceTypes type in Enum.GetValues(typeof(DeviceTypes)))
                    if (type != DeviceTypes.Invalid)
                        if (device.HasFlag(type))
                            switch (type)
                            {
                                case DeviceTypes.BlackAndWhite:
                                    return BlackAndWhite;
                                case DeviceTypes.Qvga:
                                    return Qvga;
                            }
                
                throw new ArgumentOutOfRangeException("This device is not supported by this DeviceCollection");
            }
        }
                
        #region Dispose handling
        public override void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (Disposed) return;

            if (disposing)
            {
                // Managed resources
            }

            // Unmanaged resources
            try
            {
                BlackAndWhite.Dispose();
                Qvga.Dispose();
            }
            catch { }

            Disposed = true;
        }

        ~DefaultDeviceCollection()
        {
            Dispose(false);
        }
        #endregion
    }
}
